// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using Mono.Unix;
using NLog;

namespace wxGadu
{

	/// <summary>
	/// This class keeps usefull static methods.
	/// </summary>
	public static class Helper
	{
		/// <summary>
		/// Load image from given path and scale it to desired 
		/// size if needed
		/// </summary>
		/// <param name="imagePath">
		/// A <see cref="System.String"/>
		/// Path to image (jpg, gif, png, bmp)
		/// </param>
		/// <param name="desiredSize">
		/// A <see cref="System.Int32"/>
		/// Maximum size (in pixels) of dimennsion (x or y)
		/// <remarks>
		/// Image will be scaled to given size with preserving 
		/// aspect ratio. 
		/// </remarks>
		/// </param>
		/// <returns>
		/// A <see cref="wx.Image"/>
		/// Scaled image object
		/// </returns>
		/// <exception cref="ArgumentException">
		/// When given <c>imagePath</c> doesn't lead to 
		/// valid image file.
		/// </exception>
		public static wx.Image LoadAndScale(string imagePath, int desiredSize)
		{
#if LOG
			Logger logger = LogManager.GetCurrentClassLogger();
#endif
			
			wx.Image image = new wx.Image(imagePath);
			if (image.Ok)
			{
#if LOG
				logger.Trace(Catalog.GetString("Contact photo image loaded correctly"));
#endif
				
				return LoadAndScale(image, desiredSize);
			}
			
#if LOG
			logger.Error(String.Format(Catalog.GetString("Unable to load file: {0}"), imagePath));
#endif
			throw new ArgumentException(String.Format(Catalog.GetString("Unable to load file: {0}"), imagePath));
		}
		
		/// <summary>
		/// Load image from given object and scale it to desired 
		/// </summary>
		/// <param name="image">
		/// A <see cref="wx.Image"/>
		/// Image object that should be scaled
		/// </param>
		/// <param name="desiredSize">
		/// A <see cref="System.Int32"/>
		/// Maximum size (in pixels) of dimennsion (x or y)
		/// <remarks>
		/// Image will be scaled to given size with preserving 
		/// aspect ratio. 
		/// </remarks>
		/// </param>
		/// <returns>
		/// A <see cref="wx.Image"/>
		/// Scaled image object
		/// </returns>
		/// <exception cref="ArgumentException">
		/// When given <c>imagePath</c> doesn't lead to 
		/// valid image file.
		/// </exception>
		public static wx.Image LoadAndScale(wx.Image image, int desiredSize)
		{
#if LOG
			Logger logger = LogManager.GetCurrentClassLogger();
#endif
		
			if (image.Ok)
			{
				if (image.Width > desiredSize || image.Height > desiredSize)
				{
#if LOG
					logger.Trace(Catalog.GetString("Selected photo image is too big, resizing to fit the window"));
#endif
					if (image.Width > image.Height)
					{
						double factor = (double) image.Width / (double) desiredSize;
						image = image.ScaleHQ(desiredSize, (int) (image.Height / factor));							
					}
					else
					{
						double factor = (double) image.Height / (double) desiredSize;
						image = image.ScaleHQ((int) (image.Width / factor), desiredSize);
					}
				}
				return image;
			}
#if LOG
			logger.Error(Catalog.GetString("Given image object is invalid"));
#endif
			throw new ArgumentException("image");
		}
	}
	
	[Serializable]
	public enum LogLevel
	{
		Trace = 0,
		Debug,
		Warn,
		Info,
		Error,
		Fatal,
		Off
	}
	
	public static class FormatterPaterns
	{
		public static readonly string INCOMING_PATTERN =
		"<tr>" +
			"<td bgcolor=\"#F0F0F0\">" +
			"<b>" +
				"#{sender-id} @ #{date}" +
			"</b>" +
			"<br />" +
				"#{message-body}" +
		"</td>" +
		"</tr>";
	
		public static readonly string OUTGOING_PATTERN =
		"<tr>" +
			"<td bgcolor=\"#E0E0E0\">" +
			"<b>" +
				"#{sender-id} @ #{date}" +
			"</b>" +
			"<br />" +
				"#{message-body}" +
		"</td>" +
		"</tr>";
	}
	
	public static class PatternVariables
	{
//		public static readonly string FONT_SIZE = "#{font-size}";
//		public static readonly string FONT_STYLE = "#{font-style}";
//		public static readonly string FONT_FACE = "#{font-face}";
//		public static readonly string FONT_WEIGHT = "#{font-weight}";
		public static readonly string SENDER_ID = "#{sender-id}";
		public static readonly string DATE = "#{date}";
		public static readonly string MESSAGE_BODY = "#{message-body}";
	}
	
	/// <summary>
	/// Struct used to display contact status change notification
	/// </summary>
	public struct ContactStatusChangeData
	{
		public int uin;
//		public string name;
		public string status;
	}
}
